package com.lian.servlet;

import com.alibaba.fastjson.JSON;
import com.lian.entity.work;
import com.lian.service.impl.workserviceimpl;
import com.lian.service.workservice;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
/**
 * Created by lenovo on 2018/8/11.
 */
public class pagingServlet extends HttpServlet {
    int page=0;//从第几行开始
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        workservice ser=new workserviceimpl();
        int size=2;//定义一个变量来显示多少行出来

        if(request.getParameter("page")!=null){
            page=Integer.parseInt(request.getParameter("page"));
        }
        //只有页面传来的值不为null才能转换成数字,让页面传1或者-1回来，1就是下一页，-1就是上一页
        if(request.getParameter("bz")!=null){
            int bz=Integer.parseInt(request.getParameter("bz"));
            page=page+Integer.parseInt(request.getParameter("bz"));
        }else{
            page = 0;
        }
        if(page<0){ //限制页码不能小于0
            page=0;
        }
        int count=ser.findCount();//总长度
        int maxpage=(count+size-1)/size;
        //限制最大页码不能超过查出来的页码
        if(page>maxpage-1){
            page=maxpage-1;
        }
        List<work>list=ser.findwork(page*size, size);
        String json = JSON.toJSONString(list);
        out.print(json);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
